<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: UpRank.php
 * Date: 2017/1/2
 * Time: 23:13
 */
namespace app\api\model\dao;

use think\Model;
use think\Db;

class ContributionRank extends Model
{
    protected $table = 'contribution_rank';

    protected $pk = 'id';

    /**
     * 增加贡献值
     *
     */
    public function add($cir_id, $userid, $value)
    {
        $sql = sprintf("insert into %s(cir_id, userid, value, create_time) values(%d,%d,%d,%d) on duplicate key update value=value+%d", $this->table, $cir_id, $userid, $value, time(), $value);
        return Db::table($this->table)->query($sql);
    }

    /**
     * 获取人气排行
     *
     */
    public function getRank($cir_id, $top = 10)
    {
        return Db::table($this->table)->where(['cir_id' => ['=', $cir_id]])->order('value desc')->limit(0, 10)->select();
    }

}
